#!/bin/bash
#
# This script upload TrustWalletCore.xcframework to s3 and publish to pod trunk
#

set -e

VERSION=$1
if [ -z "$VERSION" ]; then
    VERSION=`git describe --long --tags | cut -f 1 -d "-"`
fi
echo Build version "$VERSION"

# Defines
BUILD_FOLDER=build/ios-frameworks
S3_BUCKET=wallet-core

# Build first
tools/ios-build-framework

function releaseXCFramework() {
    FRAMEWORK_FILE=TrustWalletCore.xcframework
    ZIP_FILENAME="TrustWalletCore.xcframework-$VERSION.zip"

    PODSPEC_NAME=WalletCoreFW
    PODSPEC_FILE="$BUILD_FOLDER/$PODSPEC_NAME.podspec"

    # Archive
    echo Zip "$ZIP_FILENAME"
    pushd "$BUILD_FOLDER"
    rm -rf "$ZIP_FILENAME"
    zip --symlinks -r "$ZIP_FILENAME" "$FRAMEWORK_FILE"
    popd

    # Upload to S3
    echo Upload "$ZIP_FILENAME" to S3
    aws s3 cp "$BUILD_FOLDER/$ZIP_FILENAME" s3://$S3_BUCKET/ --acl public-read

    cat << EOF > "$PODSPEC_FILE"
Pod::Spec.new do |s|
  s.name             = '$PODSPEC_NAME'
  s.version          = '$VERSION'
  s.summary          = 'Trust Wallet core data structures and algorithms.'
  s.homepage         = 'https://github.com/trustwallet/wallet-core'
  s.license          = 'MIT'
  s.author           = { 'hewigovens' => '360470+hewigovens@users.noreply.github.com' }
  s.source           = { :git => 'https://github.com/trustwallet/wallet-core.git', :tag => s.version.to_s }
  s.social_media_url = 'https://twitter.com/wallet_core'
  s.source = {
    http: 'https://s3.amazonaws.com/$S3_BUCKET/$ZIP_FILENAME'
  }
  s.vendored_frameworks = 'TrustWalletCore.xcframework'
  s.ios.deployment_target = '11.0'
  s.osx.deployment_target = '10.12'
  s.swift_version = '5.1'
  s.dependency 'SwiftProtobuf'
end
EOF
    # Upload to Cocoapod
    echo "Done. running 'pod trunk push --allow-warnings $PODSPEC_FILE'"
    pod trunk push --allow-warnings "$PODSPEC_FILE"
}

function releaseTypes() {

    ZIP_FILENAME="TrustWalletCoreTypes-$VERSION.zip"

    PODSPEC_NAME=WalletCoreTypes
    PODSPEC_FILE="$BUILD_FOLDER/$PODSPEC_NAME.podspec"

    # Archive
    echo Zip "$ZIP_FILENAME"
    rm -rf "$BUILD_FOLDER/$ZIP_FILENAME"
    zip "$ZIP_FILENAME" swift/Sources/Generated/Enums/*.swift swift/Sources/Generated/Protobuf/*.swift
    mv "$ZIP_FILENAME" "$BUILD_FOLDER"

    # Upload to S3
    echo Upload "$ZIP_FILENAME" to S3
    aws s3 cp "$BUILD_FOLDER/$ZIP_FILENAME" s3://$S3_BUCKET/ --acl public-read

    cat << EOF > "$PODSPEC_FILE"
Pod::Spec.new do |s|
  s.name             = '$PODSPEC_NAME'
  s.version          = '$VERSION'
  s.summary          = 'Trust Wallet Core defines and types'
  s.homepage         = 'https://github.com/trustwallet/wallet-core'
  s.license          = 'MIT'
  s.author           = { 'hewigovens' => '360470+hewigovens@users.noreply.github.com' }
  s.source           = { :git => 'https://github.com/trustwallet/wallet-core.git', :tag => s.version.to_s }
  s.social_media_url = 'https://twitter.com/wallet_core'
  s.source = {
    http: 'https://s3.amazonaws.com/$S3_BUCKET/$ZIP_FILENAME'
  }
  s.ios.deployment_target = '11.0'
  s.osx.deployment_target = '10.12'
  s.swift_version = '5.1'
  s.source_files = 
    'swift/Sources/Generated/Enums/*.swift',
    'swift/Sources/Generated/Protobuf/*.swift'
  s.dependency 'SwiftProtobuf'
end
EOF
    # Upload to Cocoapod
    echo "Done. running 'pod trunk push --allow-warnings $PODSPEC_FILE'"
    pod trunk push --allow-warnings "$PODSPEC_FILE"
}

releaseXCFramework
releaseTypes
